using System;

namespace Admin.NET.Application.GuangHeMeter.ServerSocket;

public class BaseUtil
{
	private static string GetStrFromHex(byte bHex)
	{
		int num = bHex >> 4;
		num = bHex & 0xF;
		return num.ToString("X") + num.ToString("X");
	}

	private static void HexStringToByteArray(string strVal, byte[] pData, int startIndex, int count, bool IsReverseOrder)
	{
		strVal = strVal.Replace(" ", "");
		for (int i = 0; i < count; i++)
		{
			pData[startIndex + i] = 0;
		}
		int num = 0;
		if (strVal.Length % 2 == 1)
		{
			strVal = "0" + strVal;
		}
		if (IsReverseOrder)
		{
			for (int num2 = strVal.Length; num2 >= 2; num2 -= 2)
			{
				pData[startIndex + num] = Convert.ToByte(strVal.Substring(num2 - 2, 2), 16);
				num++;
			}
		}
		else
		{
			for (int num2 = 0; num2 < strVal.Length; num2 += 2)
			{
				pData[startIndex + num] = Convert.ToByte(strVal.Substring(num2, 2), 16);
				num++;
			}
		}
	}

	public static byte[] GetTransbyte(byte[] databuf, byte[] bytes, string imei)
	{
		int num = 0;
		byte[] array = new byte[4096];
		array[num++] = 104;
		array[num++] = 0;
		array[num++] = 0;
		array[num++] = 0;
		array[num++] = 0;
		array[num++] = 104;
		array[num++] = 75;
		string text = imei.Substring(0, 4);
		array[num++] = GetByteFromstrHex(text.Substring(2, 2));
		array[num++] = GetByteFromstrHex(text.Substring(0, 2));
		string text2 = imei.Substring(4, 5);
		int num2 = int.Parse(text2);
		array[num++] = (byte)(num2 % 256);
		array[num++] = (byte)(num2 / 256);
		string text3 = GetStrFromHex((byte)(num2 % 256)) + GetStrFromHex((byte)(num2 / 256));
		array[num++] = 26;
		array[num++] = 16;
		array[num++] = 98;
		array[num++] = 0;
		array[num++] = 0;
		array[num++] = 1;
		array[num++] = 0;
		for (int i = 0; i < databuf.Length; i++)
		{
			array[num++] = databuf[i];
		}
		array[num++] = (byte)(bytes.Length % 256);
		array[num++] = (byte)(bytes.Length / 256);
		for (int i = 0; i < bytes.Length; i++)
		{
			array[num++] = bytes[i];
		}
		HexStringToByteArray("0000", array, num, 16, IsReverseOrder: true);
		num += 16;
		byte[] array2 = new byte[4096];
		for (int j = 0; j < array.Length - 6; j++)
		{
			array2[j] = array[j + 6];
		}
		array[num++] = CheckSum(array2, 0, array2.Length);
		array[num++] = 22;
		int num5 = num - 8;
		int num6 = num5 * 4 % 65536 + 2;
		array[1] = (array[3] = (byte)(num6 % 256));
		array[2] = (array[4] = (byte)(num6 / 256));
		byte[] array3 = new byte[num];
		for (int i = 0; i < num; i++)
		{
			array3[i] = array[i];
		}
		return array3;
	}

	public static byte[] Get3761Data1(byte[] DataBlock, string imei)
	{
		int num = 0;
		byte[] array = new byte[1024];
		array[num++] = 104;
		array[num++] = 0;
		array[num++] = 0;
		array[num++] = 0;
		array[num++] = 0;
		array[num++] = 104;
		array[num++] = 75;
		string text = imei.Substring(0, 4);
		array[num++] = GetByteFromstrHex(text.Substring(2, 2));
		array[num++] = GetByteFromstrHex(text.Substring(0, 2));
		string text2 = imei.Substring(4, 5);
		int num2 = int.Parse(text2);
		array[num++] = (byte)(num2 % 256);
		array[num++] = (byte)(num2 / 256);
		array[num++] = 26;
		array[num++] = 12;
		array[num++] = 98;
		for (int i = 0; i < DataBlock.Length; i++)
		{
			array[num++] = DataBlock[i];
		}
		byte[] array2 = new byte[1024];
		for (int j = 0; j < array.Length - 6; j++)
		{
			array2[j] = array[j + 6];
		}
		array[num++] = CheckSum(array2, 0, array2.Length);
		array[num++] = 22;
		int num3 = num - 8;
		int num4 = num3 * 4 % 65536 + 2;
		array[1] = (array[3] = (byte)(num4 % 256));
		array[2] = (array[4] = (byte)(num4 / 256));
		byte[] array3 = new byte[num];
		for (int i = 0; i < num; i++)
		{
			array3[i] = array[i];
		}
		return array3;
	}

	public static byte[] Get3761Data2(byte[] DataBlock, string imei)
	{
		int num = 0;
		byte[] array = new byte[1024];
		array[num++] = 104;
		array[num++] = 0;
		array[num++] = 0;
		array[num++] = 0;
		array[num++] = 0;
		array[num++] = 104;
		array[num++] = 75;
		string text = imei.Substring(0, 4);
		array[num++] = GetByteFromstrHex(text.Substring(2, 2));
		array[num++] = GetByteFromstrHex(text.Substring(0, 2));
		string text2 = imei.Substring(4, 5);
		int num2 = int.Parse(text2);
		array[num++] = (byte)(num2 % 256);
		array[num++] = (byte)(num2 / 256);
		array[num++] = 26;
		array[num++] = 12;
		array[num++] = 98;
		array[num++] = byte.MaxValue;
		array[num++] = 0;
		array[num++] = 1;
		array[num++] = 20;
		for (int i = 0; i < 3; i++)
		{
			array[num++] = DataBlock[i];
		}
		byte[] array2 = new byte[1024];
		for (int j = 0; j < array.Length - 6; j++)
		{
			array2[j] = array[j + 6];
		}
		array[num++] = CheckSum(array2, 0, array2.Length);
		array[num++] = 22;
		int num3 = num - 8;
		int num4 = num3 * 4 % 65536 + 2;
		array[1] = (array[3] = (byte)(num4 % 256));
		array[2] = (array[4] = (byte)(num4 / 256));
		byte[] array3 = new byte[num];
		for (int k = 0; k < num; k++)
		{
			array3[k] = array[k];
		}
		return array3;
	}

	public static byte CheckSum(byte[] Buf, int begin, int Len)
	{
		byte b = 0;
		for (int i = begin; i < begin + Len; i++)
		{
			b += Buf[i];
		}
		return b;
	}

	public static byte GetByteFromstrHex(string pChar)
	{
		byte b = 0;
		if (pChar.Length > 0)
		{
			b = (byte)((pChar[0] >= '0' && pChar[0] <= '9') ? ((byte)(pChar[0] - 48)) : ((pChar[0] >= 'A' && pChar[0] <= 'F') ? ((byte)(pChar[0] - 65 + 10)) : ((pChar[0] >= 'a' && pChar[0] <= 'f') ? ((byte)(pChar[0] - 97 + 10)) : 0)));
		}
		if (pChar.Length <= 1)
		{
			return b;
		}
		if (pChar[1] >= '0' && pChar[1] <= '9')
		{
			return (byte)(b * 16 + (pChar[1] - 48));
		}
		if (pChar[1] >= 'A' && pChar[1] <= 'F')
		{
			return (byte)(b * 16 + (pChar[1] - 65) + 10);
		}
		if (pChar[1] >= 'a' && pChar[1] <= 'f')
		{
			return (byte)(b * 16 + (pChar[1] - 97) + 10);
		}
		return (byte)(b * 16);
	}
}
